new Vue({
  el: '#app',
  data() {
    return {
      form: {
        Article_Name: "",
        Category_SerialNumber: "",
        page: 1,			//页数
        limit: 10			//每页数据条数
      },
      checkboxGroup2: [],
      cities: ['本企业', '外企已发布', '外企'],
      Release: '0',
      formData: {},
      dialogFormVisible: false,
      Multistage: [{
        id: '',
        Article_SerialNumber: '',
        Unit_Price: '',
        Quantity: '',
      }],
      tableData: [{
      }],
      tablePic: [],
      Pic: [],
      loading: false,
      total: 1,
      dialogForm: false,
      title: '',
      multipleSelection: [],
      Enterprise_Info_SerialNumber: $.cookie("enterprise")
    }
  },
  mounted() {
    this.getList(this.form)
  },
  methods: {
    onSerach() {				//查询方法
      this.form.page = 1;
      this.getList(this.form)
    },
    addContract() {
      console.log("跳转")
    },
    handleSizeChange(val) {
      this.form.page = 1;
      this.form.limit = val;
      this.getList(this.form)
    },
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList(this.form)
    },
    getList(data) {
      let _this = this
      $.ajax({
        type: 'POST',
        data,
        url: DomainName + '/getArticleInfo3Action?token=' + $.cookie("token"),
        success: function (data) {
          _this.tableData = data.tableData;
          _this.tableData.forEach(item=>{
            item.loading = false
          })
          _this.total = parseInt(data.limit)
          console.log(_this.tableData)
        },
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = '../../../' + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
      // $.ajax({
      //   type: 'POST',
      //   data: { Data: "article_pic" },
      //   url: DomainName + '/GoodsFileInquiry',
      //   success: function (data) {
      //     console.log(data)
      //     data.forEach(item => {
      //       item.Picture_Url = DomainName + item.Picture_Url.replace('https://hospital-one.essc.top', '')
      //     })
      //     _this.tablePic = data;
      //   },
      //   dataType: "json"
      // });
    },
    handlePreview(Article_SerialNumber) {
      this.Pic = []
      this.tablePic.filter(item => {
        return item.Picture_Type == '0' && item.Article_SerialNumber == Article_SerialNumber
      }).forEach(item => {
        this.Pic.push(item.Picture_Url)
      })
      console.log(this.Pic);
    },
    addEnterprise() {
      window.location.href = './Add.html'
    },
    // 查看详情
    showMember(Article_SerialNumber) {
      sessionStorage.setItem("Article_SerialNumber", Article_SerialNumber)
      window.location.href = './Edit.html?Article_SerialNumber=' + Article_SerialNumber
    },
    // editMember(Article_SerialNumber) {
    //   sessionStorage.setItem("Article_SerialNumber", Article_SerialNumber)
    //   window.location.href = './Edit.html?Article_SerialNumber=' + Article_SerialNumber
    // },
    // 下架
    deletMember(row) {
      const _this = this
      row.loading = true
      let detel = {};
      $.ajax({
        url: DomainName + '/ArticleInfo_Original',
        type: 'POST',
        data: { Data: row.Article_SerialNumber },
        success: function (res) {
          if (res.length == 1) {
            if (res[0].Row_Status == '0') {
              res[0].Row_Status = '1'
              detel = res[0];
              let data = { Data: JSON.stringify({ Modify: { article_info: [detel] } }) };
              $.ajax({
                url: DomainName + '/GoodsFileEdit',
                type: 'POST',
                data,
                success: function (res) {
                  if (res.Status === 'success') {
                    row.loading = false
                    _this.$message.success("下架成功");
                    _this.getList(_this.form);
                  } else {
                    row.loading = false
                    _this.$message.error("操作失败，请稍后再试")
                  }
                },
                error: function () {
                  row.loading = false
                  _this.$message.error("操作失败，请稍后再试")
                },
                complete: function () { row.loading = false }
              })
            } else {
              res[0].Row_Status = '0'
              detel = res[0];
              let data = { Data: JSON.stringify({ Modify: { article_info: [detel] } }) };
              $.ajax({
                url: DomainName + '/GoodsFileEdit',
                type: 'POST',
                data,
                success: function (res) {
                  if (res.Status === 'success') {
                    row.loading = false
                    _this.$message.success("上架成功");
                    _this.getList(_this.form);
                  } else {
                    row.loading = false
                    _this.$message.error("操作失败，请稍后再试")
                  }
                },
                error: function () {
                  row.loading = false
                  _this.$message.error("删操作败，请稍后再试")
                },
                complete: function () { row.loading = false }
              })
            }
          }
        }
      })

    },
    add_article_specifications() {
      this.Multistage.push({
        id: '',
        Article_SerialNumber: '',
        Unit_Price: '',
        Quantity: '',
      })
    },
    remove_article_specifications() {
      if (this.Multistage.length > 1) {
        this.Multistage.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    // 公布
    stateMember(row,i) {
      this.WholesaleById(row,i)
    },
    WholesaleById(row,i) {//通过主键获取批发价格单
      row.loading= true
      console.log(row)
      let _this = this
      if (row.Release_Type == '0'||i==1) {
        $.ajax({
          url: DomainName + '/getArticleWholesale?token=' + $.cookie("token"),
          type: 'POST',
          data: {
            Article_SerialNumber: row.Article_SerialNumber,
          },
          success: function (res) {
            console.log(res.Data)
            if (res.Data.length > 0) {
              //查原值
              if(i==0){
                _this.getArticleInfoOriginal(row)
              }else{
                sessionStorage.setItem('GoodsData',JSON.stringify(res.Data[0]))
                sessionStorage.setItem('Goods',JSON.stringify(row))
                window.location.href = './SupplierGoods.html'
              }
            } else {
              _this.$confirm('还没有添加批发价格单, 是否添加?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                center: true
              }).then(() => {
                window.location.href = '../../../view/ShopManage/PriceManage/WholesalePrice/Add.html'
              }).catch(() => {
                _this.loading = false
                _this.$message({
                  type: 'info',
                  message: '已取消'
                });
              });
            }
          }
        })
      } else {
        _this.getArticleInfoOriginal(row)
      }
    },
    getArticleInfoOriginal(row) {//查原值
      let _this = this
      $.ajax({
        url: DomainName + '/getInquiryIDOriginal?token=' + $.cookie("token"),
        type: 'POST',
        data: {
          Primary_Key_Name: 'Article_SerialNumber',
          Primary_Key: row.Article_SerialNumber,
          Data: 'article_info',
        },
        success: function (res) {
          console.log(res)
          if (res.length == 1) {
            if (res[0].Release_Type == '0') {
              res[0].Release_Type = '1'
              _this.Edit(res[0],row)
            } else {
              res[0].Release_Type = '0'
              _this.Edit(res[0],row)
            }
          }else{
            row.loading = false
          }
        }
      })
    },
    Edit(detel,row) {//修改
      let _this = this
      let data = { Data: JSON.stringify({ Modify: { article_info: [detel] } }) };
      $.ajax({
        url: DomainName + '/GoodsFileEdit',
        type: 'POST',
        data,
        success: function (res) {
          if (res.Status === 'success') {
            _this.$message.success("发布成功");
            _this.getList(_this.form);
          } else {
            _this.$message.error("操作失败，请稍后再试")
          }
        },
        error: function () {
          row.loading = false
          _this.$message.error("操作失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          row.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    // 删除
    omitMember(Article_SerialNumber) {
      const _this = this
      let detel = {};
      $.ajax({
        url: DomainName + '/ArticleInfo_Original',
        type: 'POST',
        data: { Data: Article_SerialNumber },
        success: function (res) {
          if (res.length == 1) {
            res[0].Row_Status = '2'
            detel = res[0];
            let data = { Data: JSON.stringify({ Modify: { article_info: [detel] } }) };
            $.ajax({
              url: DomainName + '/GoodsFileEdit',
              type: 'POST',
              data,
              success: function (res) {
                if (res.Status === 'success') {
                  _this.$message.success("删除成功");
                  _this.getList(_this.form);
                } else {
                  _this.$message.error("操作失败，请稍后再试")
                }
              },
              error: function () {
                _this.$message.error("操作失败，请稍后再试")
              },
              complete: function (XMLHttpRequest) {
                _this.loading = false
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              }
            })
          }
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 展开详细信息
    handleChildren(row) {
      _this = this
      if (!row.Children.article_specifications.length) {
        console.log(row.Children.article_specifications.length)
        $.ajax({
          url: DomainName + '/getSpecificationsAction',
          type: 'POST',
          data: {
            Article_SerialNumber: row.Article_SerialNumber,
            token: $.cookie("token")
          },

          success: function (data) {
            console.log(data)
            // data.forEach(item => {
            //   item.Picture_URL = DomainName + item.Picture_URL
            // })
            row.Children.article_specifications = data
            // console.log(expandedRows[expandedRows.length - 1].Children.article_specifications)
          },
          dataType: "json"
        });
      }

    }
  },
})

